<template>
	<view class="content">
		<view class="app-title ac">
			<view class="fl" @click="back"><image class="back" src="/static/img/back.png"></image></view>非首次入库
		</view>
		
		<view class="top-bg"></view>
		
		<view class="detail">
			<u-cell-group>
				<u-cell-item title="入库机构:" :value="org_name == '' ? '请选择' : org_name" @click="show_picker=true;columns=org_options"></u-cell-item>
				<u-cell-item title="入库车站:" :value="station_name == '' ? '请选择' : station_name" @click="show_picker = true;columns=station_options"></u-cell-item>
				<u-cell-item title="入 库 人:" :value="data_list.personId" :arrow="false"></u-cell-item>
			</u-cell-group>
			<view style="margin: 20px 0;"><b>器材列表</b></view>
			
			<u-cell-group class="device-list" v-for="(item,index) in data_list.eqtList" :key="index">
				<view @click="remove=index;show_confirm=true;">
					<u-cell-item title="器材类型:" :value="item.eqtTypeName" :arrow="false"></u-cell-item>
					<u-cell-item title="器材型号:" :value="item.eqtModelNum" :arrow="false"></u-cell-item>
					<u-cell-item title="条形码:" :value="item.zcCode" :arrow="false"></u-cell-item>
					<u-cell-item title="当前状态:" :value="item.stateName" :arrow="false"></u-cell-item>
					<u-cell-item title="实际存放位置:" :value="item.storeAddress" :arrow="false"></u-cell-item>
				</view>
			</u-cell-group>
			
			<view class="btn">
				<button class="cancel-btn" @click="codeCheck">扫码</button><button class="default-btn fr" @click="pushData">入库</button>
			</view>
		</view>
		
		<view class="full" v-show="show_picker || show_confirm"></view>
		<u-select v-model="show_picker" :list="columns" label-name="text" @confirm="onConfirm" @cancel="onCancel"></u-select>
		<u-modal v-model="show_confirm" :show-title="false" :show-cancel-button="true" :content="content" @confirm="removeDevice" @cancel="onCancel"></u-modal>
	</view>
</template>

<script>
export default{
	data() {
		return {
			content : '确认是否要删除',
			org_options : '',
			station_options : '',
			columns: [],
			data_list:{
				"departId": "", // //入库机构
				  "eqtList": [],
				  "personId": uni.getStorageSync('username') || '', //入库人
				  "stationCode": "" //入库车站
			},
			org_name : '',
			station_name:'',
			remove:'',
			show_picker :false,
			show_confirm : false
		}
	},
	created(){
		this.initOrg();
	},
	methods: {
		back() {
			uni.navigateBack();
		},
		onConfirm(value, index) {
			if(value[0].extra.type == 'org') {
				this.data_list.departId = value[0].extra.id;
				this.org_name = value[0].label;
				this.getStation(value[0].extra.id);
			}else if(value[0].extra.type == 'station'){
				this.station_name = value[0].label;
				this.data_list.stationCode = value[0].extra.value;
			}
			this.onCancel();
		},
		onCancel() {
			this.show_picker = false;
			this.show_confirm = false;
		},
		removeDevice() {
			this.data_list.eqtList.splice(this.remove,1);
			this.remove="";
			this.onCancel();
		},
		getStation(id) {
			this.fetch('GET',this.baseUrl()+'/home/getStations',{orgid:id}).then((res)=>{
				if(res.returnCode == 1){
					this.station_options = res.data;
					this.station_options.map((item)=>{
						item.text = item.NAME;
						item.extra = {type:'station',value:item.STATIONID}
					})
				}
			})
		},
		initOrg(){
			this.fetch('GET',this.baseUrl()+'/home/getOrgTreeByPerson',{personId:this.getUserId()}).then((res)=>{
				if(res.returnCode == 1) {
					this.org_options = res.data;
					this.org_options.map((item)=>{
						item.children = '';
						item.extra = {type:'org',id:item.id};
					})
				}
			})
		},
		codeCheck() {
			 // 允许从相机和相册扫码
			uni.scanCode({
			    success: (res) => {
			        if(res) {
						this.fetch('GET',this.baseUrl()+'/eqtTZcController/scanCode',{action:'3',code:res.result}).then((res)=>{
							if(res.returnCode == 1) this.data_list.eqtList = this.data_list.eqtList.concat(res.data);
						})
					}
				}
			});
		},
		pushData() {
			if(this.data_list.departId == '') return uni.showToast({title:'请选择机构',icon:'none'});
			// if(this.data_list.stationCode == '') return uni.showToast({title:'请选择车站',icon:'none'});
			if(this.data_list.eqtList.length == 0) return uni.showToast({title:'请选择器材',icon:'none'});
			this.fetch('POST',this.baseUrl()+'/eqtTZcController/noFirstInStorage',this.data_list,true).then((res)=>{
				if(res.returnCode == 1) {
					uni.showToast({title:res.message,icon:'none'});
				}else{
					uni.showToast({title:res.message,icon:'none'});
				}
			})
		}
	}
}
</script>

<style scoped>
	.detail{
		margin-top: -170px;height: auto;min-height: 83vh;
	}
	.detail>.btn{
		position: absolute;bottom: 3%;left: 0;width: 100%;padding: 0 20px;z-index: 5;
	}
	.default-btn,.cancel-btn{
		width: 48%;margin: 0 auto;display: inline-block;
	}
	.device-list{
		padding-bottom: 90px;
	}
</style>
